<?php
/**
 * This file is part of PHPlaza (http://code.google.com/p/phplaza)
 * This file is licensed under the GNU Lesser General Public License
 *      http://www.gnu.org/licenses/lgpl.html
 *
 * No warranty is given, express or implied. Use at your own risk.
 */

namespace core::services::render;
use core::Services as services;
use core::components as comp;
abstract class AbstractRenderService implements services::IRenderService
{
		const PAGE = 'page';
		const TEXTCOMPONENT = 'textcomponent';
		const FORM = 'form';
		const BUTTON = 'button';
		
    public function __construct()
    {
    }
        
    public function getRenderer(comp::Component $component)
    {
    	$renderer = $this->getRendererByType($this->getComponentType($component), $component);
    	
    	if ($renderer == null || !($renderer instanceof services::IRenderer))
    	{
    		throw new Exception('Cannot render component of type "' . $this->getComponentType($component) . '", because no renderer is defined');
    	}
    	
    	return $renderer;
    }
    
    protected function getComponentType($component)
    {
    	if ($component instanceof comp::Button)
    		return self::BUTTON;
    	
    	if ($component instanceof comp::Form)
    		return self::FORM;
    		
    	if ($component instanceof comp::TextComponent)
    		return self::TEXTCOMPONENT;
    		
    	if ($component instanceof core::Page)
    		return self::PAGE;	
    	// TODO: add class name of the component to the exception
    	throw new Exception('Cannot render component of unknown type');
    }
    
    abstract function getRendererByType($type, core::components::Component $component);
}